home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / mac / tclMacPanic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  5.5 KB  |  235 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * tclMacPanic.c --
  3.  *
  4.  *    Source code for the "panic" library procedure used in "Simple Shell";
  5.  *    other Mac applications will probably override this with a more robust
  6.  *    application-specific panic procedure.
  7.  *
  8.  * Copyright (c) 1993-1994 Lockheed Missle & Space Company, AI Center
  9.  * Copyright (c) 1995-1996 Sun Microsystems, Inc.
  10.  *
  11.  * See the file "license.terms" for information on usage and redistribution
  12.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13.  *
  14.  * SCCS: @(#) tclMacPanic.c 1.12 96/12/12 19:31:56
  15.  */
  16.  
  17.  
  18. #include <Events.h>
  19. #include <Controls.h>
  20. #include <Windows.h>
  21. #include <TextEdit.h>
  22. #include <Fonts.h>
  23. #include <Dialogs.h>
  24. #include <Icons.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28.  
  29. #include "tclInt.h"
  30.  
  31. /*
  32.  * constants for panic dialog
  33.  */
  34. #define PANICHEIGHT 150                /* Height of dialog */
  35. #define PANICWIDTH 350                /* Width of dialog */
  36. #define PANIC_BUTTON_RECT {125, 260, 145, 335}    /* Rect for button. */
  37. #define PANIC_ICON_RECT   {10, 20, 42, 52}    /* Rect for icon. */
  38. #define PANIC_TEXT_RECT   {10, 65, 140, 330}    /* Rect for text. */
  39. #define    ENTERCODE  (0x03)
  40. #define    RETURNCODE (0x0D)
  41.  
  42. /*
  43.  * The panicProc variable contains a pointer to an application
  44.  * specific panic procedure.
  45.  */
  46.  
  47. void (*panicProc) _ANSI_ARGS_(TCL_VARARGS(char *,format)) = NULL;
  48.  
  49. /*
  50.  *----------------------------------------------------------------------
  51.  *
  52.  * Tcl_SetPanicProc --
  53.  *
  54.  *    Replace the default panic behavior with the specified functiion.
  55.  *
  56.  * Results:
  57.  *    None.
  58.  *
  59.  * Side effects:
  60.  *    Sets the panicProc variable.
  61.  *
  62.  *----------------------------------------------------------------------
  63.  */
  64.  
  65. void
  66. Tcl_SetPanicProc(proc)
  67.     void (*proc) _ANSI_ARGS_(TCL_VARARGS(char *,format));
  68. {
  69.     panicProc = proc;
  70. }
  71.  
  72. /*
  73.  *----------------------------------------------------------------------
  74.  *
  75.  * MacPanic --
  76.  *
  77.  *    Displays panic info..
  78.  *
  79.  * Results:
  80.  *    None.
  81.  *
  82.  * Side effects:
  83.  *    Sets the panicProc variable.
  84.  *
  85.  *----------------------------------------------------------------------
  86.  */
  87.  
  88. static void
  89. MacPanic(
  90.     char *msg)        /* Text to show in panic dialog. */
  91. {
  92.     WindowRef macWinPtr, foundWinPtr;
  93.     Rect macRect;
  94.     Rect buttonRect = PANIC_BUTTON_RECT;
  95.     Rect iconRect = PANIC_ICON_RECT;
  96.     Rect textRect = PANIC_TEXT_RECT;
  97.     ControlHandle okButtonHandle;
  98.     EventRecord event;
  99.     Handle stopIconHandle;
  100.     int    part;
  101.     Boolean done = false;
  102.             
  103.  
  104.     /*
  105.      * Put up an alert without using the Resource Manager (there may 
  106.      * be no resources to load). Use the Window and Control Managers instead.
  107.      * We want the window centered on the main monitor. The following 
  108.      * should be tested with multiple monitors. Look and see if there is a way
  109.      * not using qd.screenBits.
  110.      */
  111.  
  112.     macRect.top = (qd.screenBits.bounds.top + qd.screenBits.bounds.bottom)
  113.     / 2 - (PANICHEIGHT / 2);
  114.     macRect.bottom = (qd.screenBits.bounds.top + qd.screenBits.bounds.bottom)
  115.     / 2 + (PANICHEIGHT / 2);
  116.     macRect.left = (qd.screenBits.bounds.left + qd.screenBits.bounds.right)
  117.     / 2 - (PANICWIDTH / 2);
  118.     macRect.right = (qd.screenBits.bounds.left + qd.screenBits.bounds.right)
  119.     / 2 + (PANICWIDTH / 2);
  120.     
  121.     macWinPtr = NewWindow(NULL, &macRect, "\p", true, dBoxProc, (WindowRef) -1,
  122.             false, 0);
  123.     if (macWinPtr == NULL) {
  124.     goto exitNow;
  125.     }
  126.     
  127.     okButtonHandle = NewControl(macWinPtr, &buttonRect, "\pOK", true,
  128.         0, 0, 1, pushButProc, 0);
  129.     if (okButtonHandle == NULL) {
  130.     CloseWindow(macWinPtr);
  131.     goto exitNow;
  132.     }
  133.     
  134.     SelectWindow(macWinPtr);
  135.     SetCursor(&qd.arrow);
  136.     stopIconHandle = GetIcon(kStopIcon);
  137.             
  138.     while (!done) {
  139.     if (WaitNextEvent(mDownMask | keyDownMask | updateMask,
  140.         &event, 0, NULL)) {
  141.         switch(event.what) {
  142.         case mouseDown:
  143.             part = FindWindow(event.where, &foundWinPtr);
  144.     
  145.             if ((foundWinPtr != macWinPtr) || (part != inContent)) {
  146.                 SysBeep(1);
  147.             } else {
  148.                 SetPortWindowPort(macWinPtr);
  149.                 GlobalToLocal(&event.where);
  150.                 part = FindControl(event.where, macWinPtr,
  151.                 &okButtonHandle);
  152.         
  153.             if ((inButton == part) && 
  154.                 (TrackControl(okButtonHandle,
  155.                     event.where, NULL))) {
  156.                 done = true;
  157.             }
  158.             }
  159.             break;
  160.         case keyDown:
  161.             switch (event.message & charCodeMask) {
  162.             case ENTERCODE:
  163.             case RETURNCODE:
  164.                 HiliteControl(okButtonHandle, 1);
  165.                 HiliteControl(okButtonHandle, 0);
  166.                 done = true;
  167.             }
  168.             break;
  169.         case updateEvt:   
  170.             SetPortWindowPort(macWinPtr);
  171.             TextFont(systemFont);
  172.             
  173.             BeginUpdate(macWinPtr);
  174.             if (stopIconHandle != NULL) {
  175.             PlotIcon(&iconRect, stopIconHandle);
  176.             }
  177.             TextBox(msg, strlen(msg), &textRect, teFlushDefault);
  178.             DrawControls(macWinPtr);
  179.             EndUpdate(macWinPtr);
  180.         }
  181.     }
  182.     }
  183.  
  184.     CloseWindow(macWinPtr);
  185.  
  186.   exitNow:
  187. #ifdef TCL_DEBUG
  188.     Debugger();
  189. #else
  190.     abort();
  191. #endif
  192. }
  193.  
  194. /*
  195.  *----------------------------------------------------------------------
  196.  *
  197.  * panic --
  198.  *
  199.  *    Print an error message and kill the process.
  200.  *
  201.  * Results:
  202.  *    None.
  203.  *
  204.  * Side effects:
  205.  *    The process dies, entering the debugger if possible.
  206.  *
  207.  *----------------------------------------------------------------------
  208.  */
  209.  
  210. #pragma ignore_oldstyle on
  211. void
  212. panic(char * format, ...)
  213. {
  214.     va_list varg;
  215.     char errorText[256];
  216.     
  217.     if (panicProc != NULL) {
  218.     va_start(varg, format);
  219.     
  220.     (void) (*panicProc)(format, varg);
  221.     
  222.     va_end(varg);
  223.     } else {
  224.     va_start(varg, format);
  225.     
  226.     vsprintf(errorText, format, varg);
  227.     
  228.     va_end(varg);
  229.     
  230.     MacPanic(errorText);
  231.     }
  232.  
  233. }
  234. #pragma ignore_oldstyle reset
  235.